package com.lsmo.android.carfans.activity.car;

import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.lsmo.android.carfans.R;
import com.lsmo.android.carfans.model.car.BrandTypeThread;
import com.lsmo.android.carfans.process.car.CarDealerProcess;
import com.lsmo.android.carfans.view.car.SelectCarView;
import com.lsmo.android.common.base.BaseActivity;
import com.lsmo.android.common.base.BaseProcess;
import com.lsmo.android.common.base.BaseTask;
import com.lsmo.android.common.base.ViewProcessCallBack;

public class CarDealerActivity extends BaseActivity 
	implements View.OnClickListener, AdapterView.OnItemClickListener,ViewProcessCallBack{
	
	public static final String TAG = SelectCarView.class.getSimpleName();
	
	private Button button[] = new Button[3];
	private ArrayList<BrandTypeThread> brandTypeThread;
	private ArrayList<BrandTypeThread> fourSlist;//4S店
	private ArrayList<BrandTypeThread> tempList;
	private ArrayList<BrandTypeThread> zongheList;//综合店
	private ArrayList<BrandTypeThread> allList;//全部店
	private ListView mListView;
	
	private ViewGroup mProgressBarLayout;
	private CarDealerProcess carDealerProcess;
	private String[] params = null;
	public static int carid;
	public static String title;
	public Context mContext;
	private int flag;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.carDealerProcess = new CarDealerProcess();
		this.mContext = CarDealerActivity.this;
		
		carid = getIntent().getIntExtra("carid", 0);
		title = getIntent().getStringExtra("title");
		
		initView();//加载view
		
		changeButtonBg(0);//设置按钮状态
	    
		updateView();
	}
	
	
	public void initView(){
		setContentView(R.layout.car_dealer_view_comment);
		mProgressBarLayout = (ViewGroup)this.findViewById(R.id.loading_layout);
		
		button[0] = (Button)findViewById(R.id.car_dealer_brandComment_button01);
	    button[0].setText("全部");
	    button[0].setOnClickListener(this);
	    
	    button[1] = (Button)findViewById(R.id.car_dealer_brandComment_button02);
	    button[1].setText("4S店");
	    button[1].setOnClickListener(this);
	    
	    button[2] = (Button)findViewById(R.id.car_dealer_brandComment_button03);
	    button[2].setText("综合店");
	    button[2].setOnClickListener(this);
	    
	    mListView = (ListView)findViewById(R.id.listView);
	    mListView.setOnItemClickListener(this);//注册listItem监听
	}
	
	 public void changeButtonBg(int paramInt){
		 switch (paramInt) {
		 	//全部
			case 0:
				this.button[0].setText("[全部]");
				this.button[1].setText("4S");
				this.button[2].setText("综合");
				this.flag=0;
				break;
			//4S
			case 1:
				this.button[0].setText("全部");
				this.button[1].setText("[4S]");
				this.button[2].setText("综合");
				this.flag=1;
				break;
			//综合
			case 2:
				this.button[0].setText("全部");
				this.button[1].setText("4S");
				this.button[2].setText("[综合]");
				this.flag=2;
				break;
		}
	 }
	 
	 public void copyList(ArrayList<BrandTypeThread> paramArrayList){
	    int i = 0;
	    while (true)
	    {
	      int j = paramArrayList.size();
	      if (i >= j)
	        return;
	      BrandTypeThread localDealer = (BrandTypeThread)paramArrayList.get(i);
	      this.allList.add(localDealer);
	      i += 1;
	    }
	}	 
	
	private class CarDealerTask extends BaseTask{
		public static final int GET_CAR_DEALER_LIST =8;
		private CarDealerProcess carDealerProcess ;
		
		public CarDealerTask(int code, Context context, ViewProcessCallBack view,BaseProcess process) {
			super(code, context, view);
			// TODO Auto-generated constructor stub
			this.carDealerProcess = (CarDealerProcess) process;
		}
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}
		
		@Override
		protected Object doInBackground(String... params) {
			super.doInBackground(params);
			Object result = null;
	 
			switch(this.mCode){
			    
				case GET_CAR_DEALER_LIST:
					
					//加载数据
					//全部
					if(flag ==0){
						result = carDealerProcess.getCarEvaluatAllList(params, carid);
					}
					//4s
					else if(flag ==1){
						result = carDealerProcess.getCarEvaluatList(params, carid);
					}
					//综合
					else {
						result = carDealerProcess.getCarEvaluatZhList(params, carid);
					}
					
					brandTypeThread = (ArrayList<BrandTypeThread>) result;
					
					break;
			}
			return result;
		}
		
		@Override
		protected void onPostExecute(Object result) {
			super.onPostExecute(result);
			switch(this.mCode){
				 
				case GET_CAR_DEALER_LIST:
					
				 		mView.processAfterTaskFinish(this.mCode ,result );
				 	
				 	break;
		   }
		}
		
	}
	
	@Override
	public void processAfterTaskFinish(int code, Object result) {
		// TODO Auto-generated method stub
		if(result !=null){
			refreshView();
		}
		this.mProgressBarLayout.setVisibility(View.GONE);
		this.mListView.setVisibility(View.VISIBLE);
	}

	@Override
	public void updateView() {
		// TODO Auto-generated method stub
		this.mProgressBarLayout.setVisibility(View.VISIBLE);
		this.mListView.setVisibility(View.GONE);
		
			new CarDealerTask(CarDealerTask.GET_CAR_DEALER_LIST,
				this.mContext,
				this,
				this.carDealerProcess 
		).execute(new String[]{});
	}

	/*
	 * 刷新页面数据
	 */
	public void refreshView(){
		if(brandTypeThread !=null){
			CarDealerAdapter carDealerAdapter = new CarDealerAdapter(this);
			carDealerAdapter.setList(brandTypeThread);
			
			mListView.setAdapter(carDealerAdapter);
		}
	} 

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		//全部
		case R.id.car_dealer_brandComment_button01:
			changeButtonBg(0);
			updateView();
			break;
		//4s店
		case R.id.car_dealer_brandComment_button02:
			changeButtonBg(1);
			updateView();
			break;
		//综合店
		case R.id.car_dealer_brandComment_button03:
			changeButtonBg(2);
			updateView();
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		
	}

}
